<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../javascript/jquery.min.js"></script>
    <script>


    </script>

</head>


<body>
    <form class="layui-form layui-form-pane" action="" style="margin-top: 30px;">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" id="username" autocomplete="off" lay-verify="title"
                    placeholder="请输入用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="text" name="password" id="password" placeholder="请输入密码" lay-verify="pass"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-input-item">
            <input type="radio" name="rol" id="admin" value="管理员" title="管理员" checked>
            <input type="radio" name="rol" id="user" value="普通用户" title="普通用户">
        </div>

        <div class="layui-form-item layui-form-mid">
            <div class="layui-form-item">

            </div>
            <button class="layui-btn" lay-submit="" style="position:absolute;left:140px;" lay-filter="demo">提交</button>
        </div>

    </form>

    <script>
        layui.use(['form', 'layedit', 'laydate'], function () {
            let tag = window.location.href.split("=");
            if (tag.length == 3 && tag[1] == "update") {
                let id = window.location.href.split("=")[2];
                var u = "http://127.0.0.1:8088/user/find?id=" + id;
                $.get(u, function (data, status) {
                    console.log(u)
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        alert(ret.msg);
                    } else {
                        $("#username").val(ret.data.username)
                        $("#password").val(ret.data.password)

                        if (ret.data.role = "管理员") {
                            $("#admin").attr("checked", "true")

                        } else {

                            $("#user").attr("checked", "true")
                        }
                    }
                });
            }
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //创建一个编辑器
            var editIndex = layedit.build('LAY_demo_editor');

            //自定义验证规则
            form.verify({
                title: function (value) {
                    if (value = null || value.length == 0) {
                        return '名字不能为空';
                    }
                }
                , pass: [
                    /^[\S]{6,12}$/
                    , '密码必须6到12位，且不能出现空格'
                ]
                , content: function (value) {
                    layedit.sync(editIndex);
                }
            });
            //监听提交
            form.on('submit(demo)', function (data) {
                var url;
                if (tag.length == 2) {
                    url = "http://127.0.0.1:8088/user/add?username=" + $("#username").val() + "&password=" + $("#password").val() + "&role=" + $("input[name='rol']:checked").val();
                } else if (tag.length == 3) {
                    let id = window.location.href.split("=")[2];
                    url = "http://127.0.0.1:8088/user/update?id=" + id + "&username" + $("#username").val() + "&password=" + $("#password").val() + "&role=" + $("input[name='rol']:checked").val();
                }
                console.log(url)
                $.get(url, function (data, status) {
                    console.log(data)
                    var ret = JSON.parse(JSON.stringify(data));
                    console.log(ret)
                    if (ret.status == 1) {
                        layer.alert(ret.msg);
                    } else {
                        layer.alert(ret.msg);
                        window.parent.location.reload();//刷新父页面
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭当前iframe页面
                        parent.location.reload();//刷新父页面
                    }

                });
                return false;
            });
        });
    </script>

</body>

</html>